package pl.umk.mat.fred.unicore.urc.storagemanagement.preferences;

import org.eclipse.jface.preference.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import pl.umk.mat.fred.unicore.urc.storagemanagement.StorageManagementActivator;
import pl.umk.mat.fred.unicore.urc.storagemanagement.views.TimeMeterView;

public class StorageManagementPreferencePage
	extends FieldEditorPreferencePage
	implements IWorkbenchPreferencePage {
	
	private Display display;

	public StorageManagementPreferencePage() {
		super(GRID);
		setPreferenceStore(StorageManagementActivator.getDefault().getPreferenceStore());
		setDescription("Storage management preferences.");
		display = PlatformUI.getWorkbench().getDisplay();
	}
	
	/**
	 * Creates the field editors. Field editors are abstractions of
	 * the common GUI blocks needed to manipulate various types
	 * of preferences. Each field editor knows how to save and
	 * restore itself.
	 */
	public void createFieldEditors() {
		
		IPreferenceStore preferenceStore = StorageManagementActivator.getDefault().getPreferenceStore();
		
		Composite composit = new Composite(getFieldEditorParent(), SWT.FILL);
		composit.setLayout(new GridLayout(1, true));
		GridData compositData = new GridData();
		compositData.horizontalAlignment = SWT.FILL;
		composit.setLayoutData(compositData);
		
		Group analyticsGroup = new Group(composit, SWT.FILL);
		analyticsGroup.setText("Time measure preferences");
		GridLayout gridLayout = new GridLayout(1, true);
		analyticsGroup.setLayout(gridLayout);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		analyticsGroup.setLayoutData(gridData);
		
		IntegerFieldEditor historySizeEditor = new IntegerFieldEditor(PreferenceConstants.P_TIMES_HISTORY_SIZE,
				"Size of history times:", analyticsGroup);
		historySizeEditor.setValidRange(1, 50);
		historySizeEditor.getLabelControl(analyticsGroup).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		addField(historySizeEditor);
		
		IntegerFieldEditor testFileSizeEditor = new IntegerFieldEditor(PreferenceConstants.P_TEST_FILE_SIZE,
				"Size of test file in kilo bytes:", analyticsGroup);
		testFileSizeEditor.setValidRange(1, Integer.MAX_VALUE);
		testFileSizeEditor.getLabelControl(analyticsGroup).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		addField(testFileSizeEditor);
		
		IntegerFieldEditor measureInterval = new IntegerFieldEditor(PreferenceConstants.P_MEASURE_INTERVAL,
				"Measure interval in minutes:", analyticsGroup);
		measureInterval.getLabelControl(analyticsGroup).setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		addField(measureInterval);
		
		// COLOR GROUP
		Group colorGroup = new Group(composit, SWT.NONE);
		colorGroup.setText("Time measure colors");
		colorGroup.setLayout(new RowLayout(SWT.VERTICAL));
		
		Composite labelComposite = new Composite(colorGroup, SWT.NONE);
		labelComposite.setLayout(new GridLayout(7, false));
		GridData redLayoutData = new GridData(20, 20);
		redLayoutData.horizontalAlignment = GridData.CENTER;
		
		GridData textData = new GridData();
		textData.widthHint = 65;
		
		Text text1 = new Text(labelComposite, SWT.BORDER);
		text1.setText("0 KB/s");
		text1.setEditable(false);
		text1.setLayoutData(textData);
		
		Label redLabel = new Label(labelComposite, SWT.NONE);
		redLabel.setBackground(TimeMeterView.RED_COLOR);
		redLabel.setLayoutData(redLayoutData);
		
		final Text text2 = new Text(labelComposite, SWT.BORDER);
		text2.setEditable(false);
		text2.setText(preferenceStore.getInt(PreferenceConstants.P_RED_YELLOW_COLOR) + " KB/s");
		text2.setLayoutData(textData);
		
		Label yellowLabel = new Label(labelComposite, SWT.NONE);
		yellowLabel.setBackground(TimeMeterView.YELLOW_COLOR);
		yellowLabel.setLayoutData(redLayoutData);
		
		final Text text3 = new Text(labelComposite, SWT.BORDER);
		text3.setEditable(false);
		text3.setText(preferenceStore.getInt(PreferenceConstants.P_YELLOW_GREEN_COLOR) + " KB/s");
		text3.setLayoutData(textData);
		
		Label greenLabel = new Label(labelComposite, SWT.NONE);
		greenLabel.setBackground(TimeMeterView.GREEN_COLOR);
		greenLabel.setLayoutData(redLayoutData);
		
		Text text4 = new Text(labelComposite, SWT.BORDER);
		text4.setText((new Character('\u221E')).toString());
		text4.setEditable(false);
		text4.setLayoutData(textData);
		
		Composite scaleComposite = new Composite(colorGroup, SWT.FILL);
		scaleComposite.setLayout(new GridLayout(4, false));
		GridData scaleGridData = new GridData(GridData.FILL_HORIZONTAL);
		scaleGridData.widthHint = 200;
		
		final ScaleFieldEditor redYellowScale = new ScaleFieldEditor(PreferenceConstants.P_RED_YELLOW_COLOR, "Red-Yellow limit:",
				scaleComposite, 1, preferenceStore.getInt(PreferenceConstants.P_YELLOW_GREEN_COLOR) - 1, 1, 10);
		redYellowScale.getScaleControl().addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				text2.setText(redYellowScale.getScaleControl().getSelection() + " KB/s");
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {}
		});
		addField(redYellowScale);
		redYellowScale.getScaleControl().setLayoutData(scaleGridData);
		
		final ScaleFieldEditor yellowGreenScale = new ScaleFieldEditor(PreferenceConstants.P_YELLOW_GREEN_COLOR, "Yellow-Green limit:", scaleComposite, 2, 5120, 1, 10);
		yellowGreenScale.getScaleControl().addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				int selection = yellowGreenScale.getScaleControl().getSelection();
				text3.setText(selection - 1 + " KB/s");
				redYellowScale.getScaleControl().setMaximum(selection);
				text2.setText(redYellowScale.getScaleControl().getSelection() + " KB/s");
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {}
		});
		addField(yellowGreenScale);
		yellowGreenScale.getScaleControl().setLayoutData(scaleGridData);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}
	
}